package org.lq.dlz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.lq.dlz.common.Page;
import org.lq.dlz.dao.CategoryDao;
import org.lq.dlz.dao.ProductDao;
import org.lq.dlz.dao.impl.CategoryDaoImpl;
import org.lq.dlz.dao.impl.ProductDaoImpl;
import org.lq.dlz.entity.Category;
import org.lq.dlz.entity.Product;
import org.lq.dlz.entity.dto.CategoryDto;
import org.lq.dlz.service.CategoryService;

import com.google.gson.Gson;

public class CategoryServiceImpl implements CategoryService {
	CategoryDaoImpl categorydao = new CategoryDaoImpl();
	ProductDao productdao = new ProductDaoImpl();
	
	
	@Override
	public List<Category> findparent() {

		return categorydao.findByParent();
	}



	@Override
	public boolean saveCategory(Category category) {
		
		return categorydao.add(category)>0;
	}



	@Override
	public boolean UpdateCategory(Category category) {
		// TODO Auto-generated method stub
		return categorydao.update(category)>0;
	}



	@Override
	public int deleteCategoryById(int id) {
		// TODO Auto-generated method stub
		List<Product> prolist = categorydao.getCategoryTop(id);
		if(prolist.size()>0) {
			return -1;
		}else {
			return categorydao.delete(id);
		}
		
	}


/**
 * 生成树状菜单
 */
	@Override
	public String getZtreeData() {
		// TODO Auto-generated method stub
		int totalCount=categorydao.getTotalCount();
		//展示全部
		Page<Category> page = new Page<Category>(1, totalCount, totalCount);
		categorydao.findPage(page);
		
		List<CategoryDto> jsonData = new ArrayList<CategoryDto>();
//		jsonData.add(CategoryDto.builder().name("商品分类").open(true).build());
//			for (Category category : page.getList()) {
//				jsonData.add(CategoryDto.builder().id(category.getId()).name(category.getName()).parent_id(category.getParent_id()).build());
//			}
		return new Gson().toJson(jsonData);
	}


/**
 * 获取子类数量
 */
	@Override
	public Category getCategoryById(int id) {
		Category category = categorydao.findById(id);
		category.setChildCount(categorydao.findByParentId(id).size());
		return category;
	}


/**
 * 展示分类
 */
	@Override
	public List<Category> HomeCategoryList() {
		List<Category> list = categorydao.findByParent();
		List<CategoryDto> listDtos = new ArrayList<CategoryDto>();
		for (Category category : list) {
			
			category.setChildren(categorydao.findByParentId(category.getId()));
			category.setHomeProduct(categorydao.getCategoryTop(category.getId()));
		}
		return list;
	}



	@Override
	public void getProductByCid(int cid, Page<Product> page) {
		categorydao.getCategoruByCid(cid, page);
	}



	@Override
	public int getCategoruByCidCount(int cid) {
		// TODO Auto-generated method stub
		return categorydao.getCategoruByCidCount(cid);
	}



	@Override
	public List<Product> getCategoryTop(int parentid) {
		// TODO Auto-generated method stub
		return categorydao.getCategoryTop(parentid);
	}



	@Override
	public List<Category> findByParentId(int parentid, Page<Category> page) {
		return categorydao.findByParentId(parentid, page);
	}
	
	public List<CategoryDto> findAllList(){
		List<Category> findByParent = categorydao.findByParent();
		List<CategoryDto> listDtos = new ArrayList<CategoryDto>();
		for (Category category : findByParent) {
			List<CategoryDto> list = getList(category.getId());
			

			CategoryDto dto = new CategoryDto();
			
			dto.setValue(category.getId());
			dto.setName(category.getName());
			dto.setChildren(list);
			
			listDtos.add(dto);
		}
		return listDtos;
	}
	
	public List<CategoryDto> getList(int pid){
		List<CategoryDto> list  =null;
		List<CategoryDto> findparentList = categorydao.getCategoryDtos(pid);
		if(findparentList.size()!=0) {
			list=findparentList;
		};
		return list;
		
	}
	
	public static void main(String[] args) {
		CategoryServiceImpl serviceImpl = new CategoryServiceImpl();
		List<CategoryDto> findAllList = serviceImpl.findAllList();
		System.out.println(new Gson().toJson(findAllList));
	}

}
